import java.util.GregorianCalendar;
import java.util.LinkedList;


public class OddAnalyser {

	protected LinkedList<OddList> list;
	protected int day;
	
	public void setDay(int day)
	{
		this.day = day;
	}
	
	public OddAnalyser()
	{
		list = new LinkedList<OddList>();
	}
	
	public LinkedList<OddList> getList()
	{
		return list;
	}
	
	public void insertBall(Ball b)
	{
		if(containsBall(b)) //if ball already exists
		{
			System.out.println("Ball exists");
		}
		else
		{
			if(containsOccurance(b)) //if balls with same occurance exists
			{
				for(OddList ol: list)
				{
					if(ol.getOccurance() == b.getOccurance(day))
					{
						ol.addBall(b);
					}
				}
			}
			else
			{
				if(list.size() == 0) //if size of the list is 0
				{
					OddList ol = new OddList(b.getOccurance(day));
					ol.addBall(b);
					list.add(ol);
				}
				else
				{
					int temp = -1;
					for(int i = 0; i < list.size(); i++)
					{
						if(b.getOccurance(day) <= list.get(i).getOccurance())
						{
							temp = i;
							break;
						}
					}
					
					if(temp == -1)
					{
						temp = list.size();
					}
					
					OddList ol = new OddList(b.getOccurance(day));
					ol.addBall(b);
					list.add(temp, ol);
				}
			}
		}
	}
	
	
	public boolean containsBall(Ball b)
	{
		boolean result = false;
		for(OddList ol : list)
		{
			for(Ball ball: ol.getBalls())
			{
				if(ball.getName() == b.getName())
				{
					result = true;
					break;
				}
			}
		}
		return result;
	}
	
	public boolean containsOccurance(Ball b)
	{
		boolean result = false;
		for(OddList ol: list)
		{
			if(ol.getOccurance() == b.getOccurance(day))
			{
				result = true;
			}
		}
		return result;
	}
	
	
	public void printData(GregorianCalendar date)
	{
		System.out.println("Total number of balls: " + getNumBalls());
		for(OddList ol: list)
		{
			System.out.println("--------------------------------------------------------------------------------------------");
			System.out.println("Balls with occurance: " + ol.getOccurance());
			System.out.println();
			for(Ball b: ol.getBalls())
			{
				if(b instanceof MainBall)
				{
					String s = "";
					s += "MainBall: " + b.getName() + " with Occurrance: " + b.getOccurance(day) + ", odd is: " + b.getOdd() + "\n";
					s += printBallDates(b);
					//s += "----------3D----------\n";
					s += (b.getThreeDMatrix().getPercentageString(date))+ "";
					//s += "----------5D----------\n";
					//s += "4D: " + b.getFourDMatrix().getAverageInterval() + "\n";
					s += (b.getFiveDMatrix().getPercentageString(date))+ "  ";
					s += b.getOddBasedOnDates() + "\n";
					System.out.println(s);
				}
				else
				{
					String s = "";
					s += "LuckyBall: " + b.getName() + " with Occurrance: " + b.getOccurance(day) + ", odd is: " + b.getOdd() + "\n";
					s += printBallDates(b);
					//s += "----------3D----------\n";
					s += (b.getThreeDMatrix().getPercentageString(date)) + "";
					//s += "4D: " + b.getFourDMatrix().getAverageInterval() + "\n";
					//s += "----------5D----------\n";
					s += (b.getFiveDMatrix().getPercentageString(date))+ " ";
					s += b.getOddBasedOnDates() + "\n";
					System.out.println(s);
				}
			}
		}
	}
	
	public String printBallDates(Ball b)
	{
		String result = "";
		for(long interval: b.daysInBetween)
		{
			result += (interval + ", ");
		}
		result += "\n";
		return result;
	}
	

	
	public int getNumBalls()
	{
		int result = 0;
		for(OddList ol: list)
		{
			result = result + ol.getBalls().size();
		}
		return result;
	}
}
